//
//  OosOptions.h
//  oos4ios
//
//  Created by Pablo Gonzalez on 3/2/11.
//  Copyright 2011 11870.com. All rights reserved.
//

#import <Foundation/Foundation.h>
#import <oos4ios/Options/OosOptionsStore.h>
#import "../../Macros/LazyPropertyMacro.h"
#import "OosOptions.h"

/**
 This macro creates the lazy initiator with the provided arguments as mappings 
 for the serialization of the Options object. It creates a simple OosOptionsImpl
 and then casts it to the correct type.
 */
#define lazyInit(type, name, ...) \
- (type) name##LazyInit \
{ \
	NSDictionary *mapping = \
	[NSDictionary dictionaryWithObjectsAndKeys: __VA_ARGS__, nil]; \
	return (type) [OosOptions options: mapping]; \
}

@interface OosOptionsStore (LazyInit)

- (OosQueryOptions *) queryOptionsLazyInit;
- (OosPageOptions *) pageOptionsLazyInit;
- (OosFieldOptions *) fieldOptionsLazyInit;
- (OosLocationOptions *) locationOptionsLazyInit;
- (OosCheckinOptions *) checkinOptionsLazyInit;
- (OosOrderOptions *) orderOptionsLazyInit;
- (OosScopeOptions *) scopeOptionsLazyInit;
- (OosFilterOptions *) filterOptionsLazyInit;
- (OosReviewOptions *) reviewOptionsLazyInit;
- (OosOfferOptions *) offerOptionsLazyInit;

@end 

@implementation OosOptionsStore

+ (OosOptionsStore *) store
{
	return [[[OosOptionsStore alloc] init] autorelease];
}

lazyReadOnlySynthesize(OosQueryOptions *, queryOptions);
lazyReadOnlySynthesize(OosPageOptions *, pageOptions);
lazyReadOnlySynthesize(OosLocationOptions *, locationOptions);
lazyReadOnlySynthesize(OosCheckinOptions *, checkinOptions);
lazyReadOnlySynthesize(OosOrderOptions *, orderOptions);
lazyReadOnlySynthesize(OosScopeOptions *, scopeOptions);
lazyReadOnlySynthesize(OosFilterOptions *, filterOptions);
lazyReadOnlySynthesize(OosFieldOptions *, fieldOptions);
lazyReadOnlySynthesize(OosReviewOptions *, reviewOptions);
lazyReadOnlySynthesize(OosOfferOptions *, offerOptions);

lazyInit(OosQueryOptions *, queryOptions, 
		 @"q", @"query");

lazyInit(OosPageOptions *, pageOptions, 
		 @"pageSize", @"count");

lazyInit(OosLocationOptions *, locationOptions,
		 @"location", @"l",
		 @"latitude", @"lat",
		 @"longitude", @"lon",
		 @"area", @"a");

lazyInit(OosCheckinOptions *, checkinOptions, nil);

lazyInit(OosOrderOptions *, orderOptions, nil);

lazyInit(OosScopeOptions *, scopeOptions, nil);

lazyInit(OosFilterOptions *, filterOptions, 
		 @"categories", @"category",
		 @"categoryGroups", @"categoryGroup",
		 @"attributes", @"attribute");

lazyInit(OosFieldOptions *, fieldOptions, nil);
lazyInit(OosReviewOptions *, reviewOptions, nil);
lazyInit(OosOfferOptions *, offerOptions, nil);

#undef lazyInit

- (NSString *) description
{
	
	return [NSString stringWithFormat: 
			@"queryOptions: %@,\n"
			@"pageOptions: %@,\n"
			@"locationOptions: %@,\n"
			@"checkinOptions: %@,\n"
			@"orderOptions: %@,\n"
			@"scopeOptions: %@,\n"
			@"filterOptions: %@,\n"
			@"fieldOptions: %@,\n"
			@"reviewOptions: %@,\n", 
            @"offerOptions: %@\n",
			[self queryOptions],
			[self pageOptions],
			[self locationOptions],
			[self checkinOptions],
			[self orderOptions],
			[self scopeOptions],
			[self filterOptions],
			[self fieldOptions],
			[self reviewOptions],
            [self offerOptions]];
}

- (void)dealloc
{
    [queryOptions release];
	[pageOptions release];
    [fieldOptions release];
	[locationOptions release];
	[checkinOptions release];
	[orderOptions release];
	[scopeOptions release];
	[filterOptions release];
	[reviewOptions release];
    [offerOptions release];
    [super dealloc];
}

@end
